package com.shop.shoporder.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.entity.order.Order;
import com.shop.shoporder.service.LogisticsService;
import com.shop.shoporder.service.OrderAddressService;
import com.shop.shoporder.service.OrderService;
import com.shop.shoporder.service.ViewAdminOrderDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/suOrder")
public class SupplierOrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    OrderAddressService orderAddressService;
    @Autowired
    LogisticsService logisticsService;
    @Autowired
    ViewAdminOrderDetailsService viewAdminOrderDetailsService;
    @ResponseBody
    @RequestMapping(value = "/List", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response List(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                JSONObject param = JSONObject.parseObject(res);
                int page =0;
                int limit =10;
                Map<String,String> mapPa=new HashMap<>();
                mapPa.put("usernoId",(String) param.get("usernoId"));
                mapPa.put("orderStatus",(String) param.get("orderStatus"));
                mapPa.put("orderNo",(String) param.get("orderNo"));
                mapPa.put("logName",(String) param.get("logName"));
                mapPa.put("phone",(String) param.get("phone"));
                PageHelper.startPage(page, limit);
                //读取数据表
                List<Order> orderList = orderService.adminSelectOrderList(mapPa,request);
                if (orderList != null&&orderList.size()>0) {
                    PageInfo<Order> pageInfo=new PageInfo<>(orderList);
                    int count=(int)pageInfo.getTotal();
                    return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), count, "", request);
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, null, "没有数据", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
            }
        }
    }

}
